package start.demo01.datatype;

import org.junit.Test;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TimeType {

    static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    static final SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

    @Test
    public void timestampTest(){
        Timestamp timestamp=new Timestamp(new Date().getTime());
        System.out.println(df.format(timestamp));
        System.out.println(timestamp);
    }

    @Test
    public void t0() {
        String textTime = String.valueOf(System.currentTimeMillis());
        Long time = new Long(textTime);
        String s = df.format(new Date());
        System.out.println(format.format(System.currentTimeMillis()));
        System.out.println(df.format(System.currentTimeMillis()));
    }
    @Test
    public void t1(){
        LocalDateTime time = LocalDateTime.now();
        System.out.println(time);
    }

    @Test
    public void localDateTest() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        DateTimeFormatter ftf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse("2019-11-28 08:52:50", ftf);//
        System.out.println("2019-11-28 08:52:50".substring(0,10)+" 00:00:00");
        System.out.println("parse:"+parse);
        System.out.println(format.format(System.currentTimeMillis()));

        Long localDateTimeToLong = Timestamp.valueOf(LocalDateTime.now()).getTime();
        System.out.println("LocalDateTime -> Long:  " + localDateTimeToLong);
        System.out.println(format.format(localDateTimeToLong));
        LocalDateTime longToLocalDateTime =
                LocalDateTime.ofInstant(Instant.ofEpochMilli(1622105197487L), ZoneId.systemDefault());
        System.out.println("Long -> LocalDateTime:  " + longToLocalDateTime);
    }

    @Test
    public void t2() throws ParseException {
        final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        String str="2021-11-24T02:39:41.659Z";
        Date date = sdf.parse(str);
        System.out.println(date.toString());

        final SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        String str2="2020-12-19T23:33:59+08:00";
        Date d2=sdf2.parse(str2);
        System.out.println(d2.toString());
    }

    @Test
    public void t3() throws ParseException {
        final SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd HH:mm:ss");
        Date date = sdf.parse("20220115 00:00:00");
        System.out.println(date.toString());
        System.out.println(date.getTime());
        System.out.println(sdf.format(date));
        System.out.println("now:"+new Date().getTime());

        System.out.println(sdf.format(new Date(1642521600000l)));
        System.out.println(sdf.format(new Date(1642176000000l)));

    }
    @Test
    public void t4(){
        System.out.println(13599647/1024/1024);
    }

}
